package service.applycheck.impl;

import java.sql.Timestamp;
import java.util.List;

import core.global.Global;

import model.creditcheck.Applystate;
import model.security.UsrUserRole;

import dao.ApplystateDao;
import dao.security.UsrUserRoleDao;

import exception.Exceptions;
import service.applycheck.ApplyStateService;
import util.Time;

public class ApplyStateServiceImpl implements ApplyStateService{
	private ApplystateDao applystateDao;
	private UsrUserRoleDao usrUserRoleDao;
	
	public UsrUserRoleDao getUsrUserRoleDao() {
		return usrUserRoleDao;
	}

	public void setUsrUserRoleDao(UsrUserRoleDao usrUserRoleDao) {
		this.usrUserRoleDao = usrUserRoleDao;
	}

	@Override
	public boolean isHaveRecord(Integer applyid) throws Exceptions {
		try {
			if(this.applystateDao.countModel("applyid = " + applyid + " and checktype <> 'A'") == 0){
				return false;
			}else{
				return true;
			}
		} catch (Exception e) {
			throw new Exceptions("查询出现错误");
		}
	}
	
	@Override
	public List<Applystate> getAllListByApplyid(Integer applyid) throws Exceptions {
		try {
			return this.applystateDao.findList("applyid", applyid);
		} catch (Exception e) {
			throw new Exceptions("查询出现异常，请检查你的操作是否符合标准!");
		}
	}

	@Override
	public void delAllRecordByApplyid(Integer applyid) throws Exceptions {
		List<Applystate> ass = this.applystateDao.findModel("applyid = " + applyid + " and checktype <> 'A'");
		try {
			if(ass != null){
				for(Applystate as : ass){
					if(as != null){
						this.applystateDao.delete(as);
					}
				}
			}
		} catch (Exception e) {
			throw new Exceptions("操作时出现异常，请联系管理员!");
		}
	}

	public ApplystateDao getApplystateDao() {
		return applystateDao;
	}

	public void setApplystateDao(ApplystateDao applystateDao) {
		this.applystateDao = applystateDao;
	}

	@Override
	public Applystate save(Applystate applystate) throws Exception{
		try{
		return this.applystateDao.save(applystate);
		}catch(Exception e){
			throw new Exceptions("在执行插入的过程中，出现了一个问题，请检查你的申请信息!");
		}
	}

	@Override
	public void saveAllByRole(Integer applyid, Integer roleid, String checkType) throws Exception {
		Timestamp nowTime = Time.CurrentTimestamp();
		
		List<UsrUserRole> userList = this.usrUserRoleDao.findList("roleid", roleid);
		for(UsrUserRole usrUserRole : userList){
			Applystate _applystate = new Applystate();
			_applystate.setApplyid(applyid);
			_applystate.setManagerroleid(roleid);
			_applystate.setChecktime(nowTime);
			_applystate.setManagerid(usrUserRole.getId());
			_applystate.setCheckstate(0);
			_applystate.setChecktype(checkType);
			this.applystateDao.save(_applystate);
		}
	}

	@Override
	public void saveAllByManagerId(Integer applyid, Integer managerid,
			String checkType, Integer applytype, int count) throws Exception {
		if(managerid != null){
			Applystate _applystate = new Applystate();
			_applystate.setApplyid(applyid);
			_applystate.setManagerid(managerid);
			_applystate.setCheckstate(0);
			_applystate.setChecktype(checkType);
			_applystate.setApplytype(applytype);
			_applystate.setCount(count);
			this.applystateDao.save(_applystate);
		}
	}

	@Override
	public List<?> findApplyStateByApplyId(Integer applyid) {
		return this.applystateDao.findModel("applyid = " + applyid + " and managerid = " + Global.userid());
	}

	@Override
	public Applystate getOneApplyStateByIdAndType(Integer id, String type) {
		return this.applystateDao.findModel("applyid = " + id + " and checktype = '" + type + "' and managerid = " + Global.userid()).get(0);
	}

	@Override
	public int isHaveFP(Integer applyid) {
		return this.applystateDao.countModel("applyid = " + applyid + " and checktype = '" + "D'");
	}

	@Override
	public int checkApplyState(Integer applyid) {
		
		/*
		 * 返回值说明：
		 * 0没有完全进行审核
		 * 1审核通过
		 * 2审核未通过
		 * */
		
		int countAll;
		int countAgreed;
		int countNotAgreed;
		int countStarted = this.applystateDao.countModel("applyid = " + applyid + " and checkstate = 0 and checktype = 'C'");
		if(countStarted == 0){
			countAgreed = this.applystateDao.countModel("applyid = " + applyid + " and checkstate = 1 and checktype = 'C'");
			countNotAgreed = this.applystateDao.countModel("applyid = " + applyid + " and checkstate = 2 and checktype = 'C'");
			countAll = this.applystateDao.countModel("applyid = " + applyid + " and checktype = 'C'");
			int hrefCount = (countAll / 2) + 1;
			if(countAgreed >= hrefCount){
				return 1;
			}else if(countNotAgreed >= hrefCount){
				return 10;
			}else{
				return 2;
			}
		}
		return 0;
	}

	@Override
	public int getBackStateCheck(Integer applyid) {
		int count = 1;
		int countGetBack = this.applystateDao.countModel("applyid = " + applyid + " and checkstate = 3 and checktype = 'C'");
		
		if(countGetBack >= count){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public boolean isAllSurveyDo(Integer applyid, String type) {
		int count = this.applystateDao.countModel("applyid = " + applyid + " and checkstate = 0 and checktype = '" + type + "'");
		if(count != 0){
			return false;
		}
		return true;
	}

	@Override
	public void delAllRecordByApplyidAndType(Integer applyid, String checktype,
			Integer type) throws Exceptions {
		List<Applystate> ass = this.applystateDao.findModel("applyid = " + applyid + " and checktype = " + checktype + " and applytype = " + type);
		try {
			if(ass != null){
				for(Applystate as : ass){
					if(as != null){
						this.applystateDao.delete(as);
					}
				}
			}
		} catch (Exception e) {
			throw new Exceptions("操作时出现异常，请联系管理员!");
		}
		
	}

	@Override
	public void resetSurveyAccord(Integer applyid) {
		List<Applystate> ass = this.applystateDao.findModel("applyid = " + applyid + " and checktype = 'A'");
		if(ass != null){
			for(Applystate as : ass){
				as.setChecktime(null);
				as.setCheckopinion(null);
				as.setCheckstate(0);
				/*if(as.getCount() == null){
					as.setCount(1);
				}else{
					as.setCount(as.getCount() + 1);
				}*/
				this.applystateDao.save(as);
			}
		}
	}
}
